/**
 * Simple ExtJS 4 Controller for linking MapView events to BoreChart updates.
 * 
 * Relies on:
 * - vvg.lib
 * 
 * @author: King Lung Chiu
 */

(function( vvg){
   
   var app = vvg.app;
   var lib = vvg.lib;
   var dbg = vvg.dbg;
   var appControllers = app.xControllers = app.xControllers || [];
   
   Ext.define('VVG.controller.ChartController', {
      extend: 'Ext.app.Controller'
      ,views: ['MapView', 'BoreChart']
      ,refs: [{
            ref: 'map'
            ,selector: 'mapview'
         },{
            ref: 'boreChart'
            ,selector: 'borechart'
      }]
      
      ,init: function() {
         var self = this;
         var VIZ_DATA_READY = lib.eventNames.VIZ_DATA_READY;
         var VIZ_DATA_FETCH_STARTED = lib.eventNames.VIZ_DATA_FETCH_STARTED;
         var VIZ_DATA_FETCH_COMPLETE = lib.eventNames.VIZ_DATA_FETCH_COMPLETE;
         var VIZ_BORE_SELECTED = lib.eventNames.VIZ_BORE_SELECTED;
         var VIZ_BORE_UNSELECTED = lib.eventNames.VIZ_BORE_UNSELECTED;

         this.control({
            /**
             * Listen for MapView's custom events.
             *
             * same as
             *    mapview: {
             *       eventName: function(...) { ... }
             *    }
             *  except code below avoids hard-coding eventName, reducing typos
             */
            mapview: lib.objFromKVPairs([
               [VIZ_DATA_READY, function(vizData){
                  self.getBoreChart().vvg_updateData(vizData);
                  lib.log(
                     '\nChartController: mapview.vizDataReady event fired!\n '
                     + vizData.debugMsg + '\n '
                     + 'number of selected bores: ' + vizData.selectedBores.length);
               }]
               ,[VIZ_DATA_FETCH_STARTED, function(event){
                  self.getBoreChart().setLoading(true, true);
                  lib.log(
                     '\nChartController: mapview.vizDataFetchStarted event fired!\n '
                     + event.debugMsg);
               }]
               ,[VIZ_DATA_FETCH_COMPLETE, function(event){
                  self.getBoreChart().setLoading(false, true);
                  lib.log(
                     '\nChartController: mapview.vizDataFetchComplete event fired!\n '
                     + event.debugMsg);
               }]
            ])
            /**
             * Listen for BoreChart's custom events.
             */
            ,borechart: lib.objFromKVPairs([
               [VIZ_BORE_SELECTED, function(boreId){
                  self.getMap().vvg_selectBore(boreId);
                  lib.log(
                     '\nChartController: borechart.vizSelectBore event fired!\n '
                     + 'bore ID: ' + boreId);
               }]
               ,[VIZ_BORE_UNSELECTED, function(boreId){
                  self.getMap().vvg_unselectBore(boreId);
                  lib.log(
                     '\nChartController: borechart.vizUnselectBore event fired!\n '
                     + 'bore ID: ' + boreId);
               }]
            ])
         });
         
         appControllers.push(this);
      }
   });
   
   // For ExtJS chart based BoreChart
//   Ext.define('VVG.controller.ChartController', {
//      extend: 'Ext.app.Controller'
//      ,views: ['MapView', 'BoreChart']
//      ,stores: ['BoreData']
//      ,refs: [{
//            ref: 'map'
//            ,selector: 'mapview'
//         },{
//            ref: 'boreChart'
//            ,selector: 'borechart'
//      }]
////      ,views: ['MapView', 'ChartView']
////      ,stores: ['ChartData']
//      
//      ,init: function() {
//         var self = this;
//         this.control({
//            mapview: {
//               vizDataReady: function(vizData) {
//                  self.getBoreDataStore().loadData(
//                     app.generateSampleAreaChartData()
//                  );
//                  
//                  lib.log(
//                     '\nChartController: mapview.vizDataReady event fired! '
//                     + vizData);
//               }
//            }
//         });
//         
//         appControllers.push(this);
//      }
//   });
   
}(window.vvg = window.vvg || {}));
